package mercury.util;

import mercury.gui.MainGUI;

import org.apache.log4j.Logger;

public class Log  {

	private Object obj;
	private Logger logger;
	
	public Log(Object obj) {
		logger = Logger.getLogger(obj.getClass());
		this.obj = obj;
	}
	
	private Object getIdentity(Object message) {
		String n = obj.getClass().getName() + "@" + Integer.toHexString(obj.hashCode());
		String v = obj.toString();
		String ret = "";
		if(!n.equals(v)) {
			
			if(message instanceof String) {
				ret += v + " - " + (String) message;
			} else {
				ret += v + " - " + message.toString();
			}
		} else {
			if(message instanceof String) {
				ret += " - " + (String) message;
			} else {
				ret += " - " + message.toString();
			}
		}
		
		MainGUI.get().appendLog(ret.toString() + "\n\r");
		
		return (Object) ret;
	}

	public void debug(Object message) {
		logger.debug(getIdentity(message));
	}

	public void debug(Object message, Throwable t) {
		logger.debug(getIdentity(message), t);
	}

	public void error(Object message) {
		MainGUI.get().appendResults("ERROR: " + message.toString() + "\n\r");
		logger.error(getIdentity(message));
	}

	public void error(Object message, Throwable t) {
		MainGUI.get().appendResults("ERROR: " + message.toString() + "\n\r");
		logger.error(getIdentity(message), t);
	}

	public void fatal(Object message) {
		logger.fatal(getIdentity(message));
	}

	public void fatal(Object message, Throwable t) {
		logger.fatal(getIdentity(message));
	}

	public void info(Object message) {
		MainGUI.get().appendResults(message.toString() + "\n\r");
		logger.info(getIdentity(message));
	}

	public void info(Object message, Throwable t) {
		MainGUI.get().appendResults(message.toString() + "\n\r");
		logger.info(getIdentity(message), t);
	}

	public void trace(Object message) {
		logger.trace(getIdentity(message));
	}

	public void trace(Object message, Throwable t) {
		logger.trace(getIdentity(message), t);
	}

	public void warn(Object message) {
		logger.warn(getIdentity(message));
	}

	public void warn(Object message, Throwable t) {
		logger.warn(getIdentity(message), t);
	}

}
